import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
// import store from "@/store"; // 引入 vuex 使用 提示组件

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home', // 路由重定向，就是打开项目默认显示的页面
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
  },
  {
    path: '*',
    name: 'Error',
    component: () => import('../views/Error.vue'),
  },
  {
    path: '/goods',
    name: 'Goods',
    component: () => import('../views/Goods.vue'),
  },
  {
    path: '/free',
    name: 'Free',
    component: () => import('../views/Free.vue'),
  },
  {
    path: '/order',
    name: 'Order',
    component: () => import('../views/Order.vue'),
  },
  {
    path: '/user',
    name: 'User',
    redirect: '/user/userCart', // 路由重定向，就是打开user默认显示的页面
    component: () => import('../views/User.vue'),
    meta: {
      isLogin: true,
    },
    children: [
      {
        path: 'userCart',
        name: 'UserCart',
        component: () => import('../components/user/userCart.vue'),
      },
      {
        path: 'userHome',
        name: 'UserHome',
        component: () => import('../components/user/userHome.vue'),
      },
      {
        path: 'userAccount',
        name: 'UserAccount',
        component: () => import('../components/user/userAccount.vue'),
      },
      {
        path: 'userAdress',
        name: 'UserAdress',
        component: () => import('../components/user/userAdress.vue'),
      },
      {
        path: 'userMessage',
        name: 'UserMessage',
        component: () => import('../components/user/userMessage.vue'),
      },
      {
        path: 'userScoreDetail',
        name: 'UserScoreDetail',
        component: () => import('../components/user/userScoreDetail.vue'),
      },
      {
        path: 'userScoreStrategy',
        name: 'UserScoreStrategy',
        component: () => import('../components/user/userScoreStrategy.vue'),
      },
    ],
  },
  {
    path: '/details',
    name: 'Details',
    component: () => import('../views/Details.vue'),
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch((err) => {})
}

// 路由前置守卫
// router.beforeEach((to, from, next) => {
// console.log(to, "---------", from);
//   let token = localStorage.getItem("x-auth-token");
//   // 判断是否需要登录
//   if (to.meta.isLogin) {
//     // 判断是否登录
//     if (token) {
//       // 登录过了放行
//       next();
//     } else {
//       // 没登录,提示请先登录
//       // 在 js 中使用 vuex
//       store.dispatch("showToast/asyncIsShowToast", {
//         msg: "请先登录!",
//         type: "warning",
//       });
//       return;
//     }
//   } else {
//     // 不需要登录,直接放行
//     next();
//   }
// });

export default router
